package lee.vioson.ebook.ui.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.navigation.NavHostController
import lee.vioson.ebook.data.AppContainer
import lee.vioson.ebook.ui.AppDestinations
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.viewmodels.HomeViewModel

/**
 *Author:viosonlee
 *Date:2023/6/3
 *DESCRIPTION:
 */
@Composable
fun HomeScreen(
    navHostController: NavHostController,
    appContainer: AppContainer,
    viewModel: HomeViewModel = viewModel(factory = viewModelFactory {
        initializer { HomeViewModel(appContainer) }
    })
) {
    val state by viewModel.state.collectAsState()

    LaunchedEffect(key1 = Unit, block = {
        viewModel.homeData(false)
    })
    Column(
        Modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState())) {
        Box {
            val bannerData by viewModel.bannerData.collectAsState()
            Banner(
                list = bannerData,
                onImageUrl = { it.imageUrl },
                onClick = {

                }
            )
            val statusBarHeight = WindowInsets.statusBars.asPaddingValues().calculateTopPadding()
            Row(
                Modifier
                    .fillMaxWidth()
                    .padding(top = statusBarHeight)
                    .padding(end = 16.dp),
                horizontalArrangement = Arrangement.End,
                verticalAlignment = Alignment.CenterVertically,
            ) {
                Icon(
                    modifier = Modifier
                        .clickable {
                            //to search
                            navHostController.navigate(AppDestinations.SEARCH_ROUTE)
                        }
                        .padding(8.dp),
                    imageVector = Icons.Rounded.Search,
                    tint = AppTheme.appColors.grayIconColor,
                    contentDescription = "search"
                )
            }
        }
        if (!state.isVip) {
            VipGuide(
                modifier = Modifier
                    .padding(vertical = 16.dp, horizontal = 8.dp)
                    .fillMaxWidth()
                    .height(70.dp),
                navHostController
            )
        }
        state.homeData?.recommendModule?.let {
            if (it.isNotEmpty) {
                HomeBookList(
                    title = it.moduleName!!,
                    list = it.dataList!!,
                    navHostController = navHostController
                )
            }
        }
        state.homeData?.hotModule?.let {
            if (it.isNotEmpty) {
                HomeBookList(
                    title = it.moduleName!!,
                    list = it.dataList!!,
                    navHostController = navHostController
                )
            }
        }
        state.homeData?.favoritesModule?.let {
            if (it.isNotEmpty) {
                HomeBookList(
                    title = it.moduleName!!,
                    list = it.dataList!!,
                    navHostController = navHostController
                )
            }
        }
    }
}

